package com.linewell.controller;

import com.linewell.entity.Article;
import com.linewell.entity.Collected;
import com.linewell.entity.Focus;
import com.linewell.entity.UserInfo;
import com.linewell.service.IArticleService;
import com.linewell.service.ICollectedService;
import com.linewell.service.IFocusService;
import com.linewell.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Title: ssm2
 * @description: 个人信息与我的主页控制器
 * @author: hyuhuang@linewell.com
 * @since:2018/2/27
 */
@Controller
public class MyHomeController {
    @Autowired
    ICollectedService collectedService;
    @Autowired
    IFocusService focusService;
    @Autowired
    IArticleService articleService;
    @Autowired
    IUserService userService;

    /**
    * @descripition 个人信息需要的材料查询
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param flag，request
    * @return
    */
    @RequestMapping("myhome")
    public String selectFive(@RequestParam(value="flag",required=false) String flag, HttpServletRequest request, Model model){
        UserInfo u = (UserInfo) request.getSession().getAttribute("LoginUser");
        //关注数  fans数
        List<Collected> list1 = collectedService.selectFive(u.getUserId());
        Focus list2 = focusService.selectFive(u.getUserId());
        List<Article> list3 = articleService.selectById(u.getUserId());
        List<Article> list4 = articleService.selectArticleWithUserId(u.getUserId());
        model.addAttribute("collected",list1);
        model.addAttribute("focus",list2);
        model.addAttribute("article",list3);
        model.addAttribute("articleList",list4);
        model.addAttribute("flag",flag);
        return "myhome";
    }

    @RequestMapping("showByUserId")
    public String showByUserId(@RequestParam(value="flag",required=false) String flag, HttpServletRequest request, Model model){
        Long userId = Long.valueOf(request.getParameter("userId"));
        //关注数  fans数
        UserInfo userInfo = userService.getUserByUserId(userId);
        List<Collected> list1 = collectedService.selectFive(userId);
        Focus list2 = focusService.selectFive(userId);
        List<Article> list3 = articleService.selectById(userId);
        List<Article> list4 = articleService.selectArticleWithUserId(userId);
        model.addAttribute("userinfo",userInfo);
        model.addAttribute("collected",list1);
        model.addAttribute("focus",list2);
        model.addAttribute("article",list3);
        model.addAttribute("articleList",list4);
        model.addAttribute("flag",flag);
        return "focusHome";
    }
}
